﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DuSQL.API;
using System.Data;

namespace DuSQL.UI
{
    /// <summary>
    /// EditSql.xaml 的交互逻辑
    /// </summary>
    public partial class EditSql : Page
    {
        public EditSql()
        {
            InitializeComponent();
        }
        private string connstr = "";
        private NodeType dbtype;
        public EditSql(string title,string connstring,NodeType databasetype)
        {
            InitializeComponent();
            connstr = connstring;
            dbtype = databasetype;
            this.Title = title;
        }        
        private void btnRunSql_Click(object sender, RoutedEventArgs e)
        {          
            lbltips.Content = "";
            try
            {
                string sql = txtContents.Text.Trim();
                switch (dbtype)
                {
                    case NodeType.DbConnection_Access:
                        {
                            
                            DataSet ds = DbHelper.Query(sql, connstr);
                            if (ds.Tables.Count > 0)
                            {
                                gvData.ItemsSource = ds.Tables[0].DefaultView;
                            }
                            break;
                        }
                    case NodeType.DbConnection_SqlServer:
                        {
                            DataSet ds=DbHelperSQL.Query(connstr, sql);
                            if (ds.Tables.Count > 0)
                            {
                                gvData.ItemsSource = ds.Tables[0].DefaultView;
                            }
                            break;
                        }
                }
                lbltips.Content = "执行成功!";
            }
            catch(Exception ex)
            {
                lbltips.Content = ex.Message;
            }
        }
    }
}
